import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

//引入axios
import axios from 'axios'


//给Vue添加axios属性
Vue.prototype.$http = axios;
Vue.prototype.$fastbaseUrl = "http://115.159.217.249:8888";
//配置axios的全局基本路径
axios.defaults.baseURL='/api';

const router = new VueRouter({
  routes
})

//使用axios前置拦截器，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");
    if(token){
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
})

//使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
    console.debug(result)
    let data = result.data;
    if(!data.success && data.message==="noLogin"){
        //删除token和用户信息
        localStorage.removeItem("token");
        localStorage.removeItem("userLogin");
        router.push("/login");
    }
    return result;
},error => {
    Promise.reject(error);
})

//路由拦截器
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login' || to.path == '/ShopReg' || to.path == '/HomePage') {
    //下一步的意思
    next();
  }else{
    //获取token
      var token = localStorage.getItem("token");
      //有值说明是登录，就放行
      if(token){
        next();
        //否在跳转到登录界面
      }else{
        next({path:"/login"});
      }
  }
})



//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

